﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using OthelloGame;
using System.Diagnostics;

namespace OthelloCognition
{
    public class ABPruneSolverVG : AbstractSolver
    {
        private AbstractHeuristic h;
        private int top;

        public ABPruneSolverVG(Heuristics heur)
        {
            h = HeuristicFactory.getHeuricstic(heur);
        }

        public override SolverEnum SolverName()
        {
            return SolverEnum.ScoreABVG;
        }

        protected override void examineBoard(OthelloGameBoard board)
        {
            move = board.AvailableOptionLocations[0];
            for (top = 2; top < 61; top += 2)
            {
                Console.WriteLine("\u03B1 - \u03B2 searching depth {0}", top);
                AlphaBeta(board, top, -1000000, 1000000);
            }
        }

        private int AlphaBeta(OthelloGameBoard board, int depth, int alpha, int beta)
        {
            if (depth == 0) return h.evaluate(board, currentPlayer);
            if (board.AvailableOptionLocations.Count == 0) return alpha;

            int max = alpha;
            foreach (OthelloBoardLocation possible_move in board.AvailableOptionLocations)
            {
                OthelloGameBoard child = expandState(board, possible_move);

                int val = (child.PlayersTurn != board.PlayersTurn)? 
                     -AlphaBeta(child, depth - 1, -beta, -max) : AlphaBeta(child, depth-1, max, beta);
          //      int val = -AlphaBeta(child, depth - 1, -beta, -max);
                if (val > max)
                {
                    if (val >= beta) return beta;
                    max = val;
                    if (depth == top)
                    {
                        move = possible_move;
                    }
                }
            }
            return max;
        }
    }
}